<?php


namespace App\Http\Controllers\Admin;
use App\Http\Controllers\BaseController;
use App\Models\Banner;
use App\Models\HomepageDialog;
use Illuminate\Http\Request;

class HomepageDialogController extends BaseController
{
    /**
     * 列表
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request) {
        $list = HomepageDialog::orderBy("id", "desc")
            ->paginate(per_page());
        return $this->paginate($list);
    }

    /**
     * 详情
     * @param Request $request
     * @return mixed
     * @throws \Illuminate\Validation\ValidationException
     */
    public function info(Request $request) {
        $this->validate($request, [
            'id' => 'required|exists:homepage_dialog'
        ]);
        $id = \request("id");
        $info = HomepageDialog::find($id);
        return $this->success($info);
    }

    /**
     * 保存|修改
     * @param Request $request
     * @return mixed
     * @throws \Illuminate\Validation\ValidationException
     */
    public function store(Request $request) {
        $this->validate($request, [
            'file_path' => 'required',
            'id' => 'filled|exists:homepage_dialog'
        ]);
        $input = $request->input();
        $id = \request("id");
        $status = \request("status", 0);
        if ($id) {
            $banner = HomepageDialog::where('id', $id)->first();
            $banner->update($input);
        } else {
            $info = HomepageDialog::create($input);
            $id = $info->id;
        }
        if ($status) {
            HomepageDialog::where('id', '!=', $id)->where('status', 1)->update([
                'status' => 0
            ]);
        }
        return $this->success($id);
    }

    public function destroy(Request $request) {
        $this->validate($request, [
            'id' => 'required|exists:homepage_dialog'
        ]);
        $id = $request->input('id');
        HomepageDialog::where('id', $id)->first()->delete();
        return $this->success([]);
    }


}

